package com.stutter.controller;

import com.stutter.util.CleanInput;
import com.stutter.util.FindRepeatedWords;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class Find extends HttpServlet {

    public void doPost(HttpServletRequest _req, HttpServletResponse _resp) throws IOException {

        String input = _req.getParameter("input");

        List<String> lines = Arrays.asList(input.split("\\r?\\n"));

        List<Character> filter = Arrays.asList(',', '.', '!', '-', '+', '=', ';', ':', '?', '&', '{', '}', '\\');

        CleanInput cleaner = new CleanInput(filter);

        List<String> clean_input = cleaner.clean(lines);

        FindRepeatedWords finder = new FindRepeatedWords(clean_input);

        finder.findRepeats();

        String repeats = finder.getRepeats();

        String output = "Sending text to the server:\n" + input + "\n\nServer replies:\n" + repeats;

        _resp.getWriter().write(output);
    }
}
